package org.ezaero.jmimic.api;

import java.io.File;

import org.ezaero.jmimic.api.checker.InvocationChecker;
import org.ezaero.jmimic.api.checker.ObjectEqualsChecker;
import org.ezaero.jmimic.impl.store.xstream.XStreamFactory;

import com.thoughtworks.xstream.XStream;

/**
 * Provides the configuration for mimicry. Setters return back an instance of this config, to allow chaining.
 * 
 * @author alex.radeski
 */
public class MimicConfig {

    private StoreFormat format;
    private XStream xstream;
    private File outputDir = new File("./mimic/");
    private InvocationChecker invocationChecker = new ObjectEqualsChecker();
    private boolean isInvocationCheckEnabled = true;
    private boolean forceOverwrite = false;
    private boolean isCompressed = false;

    public MimicConfig() {
        setFormat(StoreFormat.XML);
    }

    public MimicConfig(MimicConfig other) {
        this.format = other.format;
        this.xstream = other.xstream;
        this.outputDir = other.outputDir;
        this.invocationChecker = other.invocationChecker;
        this.isInvocationCheckEnabled = other.isInvocationCheckEnabled;
        this.forceOverwrite = other.forceOverwrite;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("MimicConfig [format=");
        builder.append(format);
        builder.append(", invocationChecker=");
        builder.append(invocationChecker);
        builder.append(", isInvocationCheckEnabled=");
        builder.append(isInvocationCheckEnabled);
        builder.append(", outputDir=");
        builder.append(outputDir);
        builder.append(", forceOverwrite=");
        builder.append(forceOverwrite);
        builder.append("]");
        return builder.toString();
    }

    /**
     * Return the storage format used. 
     */
    public StoreFormat getFormat() {
        return format;
    }

    /**
     * Set the storage format. The default is {@link StoreFormat#XML}
     * @param format the mimic format.
     */
    public MimicConfig setFormat(StoreFormat format) {
        this.format = format;
        this.xstream = XStreamFactory.create(format);
        return this;
    }

    /**
     * Return the underlying XStream instance. This is provided so you can configure some aspect of xstream, such as aliaes or converters.
     */
    public XStream getXStream() {
        return xstream;
    }

    /**
     * Return the output directory.
     */
    public File getOutputDir() {
        return outputDir;
    }

    /**
     * Set the output directory. Mimic files will be created in here. The default is <code>./mimic</code>.
     * @param outputDir
     * @return
     */
    public MimicConfig setOutputDir(File outputDir) {
        this.outputDir = outputDir;
        return this;
    }

    public InvocationChecker getInvocationChecker() {
        return invocationChecker;
    }

    public void setInvocationChecker(InvocationChecker invocationChecker) {
        this.invocationChecker = invocationChecker;
    }

    public boolean isInvocationCheckEnabled() {
        return isInvocationCheckEnabled;
    }

    public void setInvocationCheckEnabled(boolean isInvocationCheckEnabled) {
        this.isInvocationCheckEnabled = isInvocationCheckEnabled;
    }

    public boolean isForceOverwrite() {
        return forceOverwrite;
    }

    public void setForceOverwrite(boolean overwriteMimicFile) {
        this.forceOverwrite = overwriteMimicFile;
    }

    public boolean isCompressed() {
        return isCompressed;
    }

    public void setCompressed(boolean compressOutput) {
        this.isCompressed = compressOutput;
    }
}
